package com.jwtproject.bootserver.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.jwtproject.bootserver.status.ProjectStatusEnum;

import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

public class UtilResponse {
    public UtilResponse() {
    }

    public static void writeResult(HttpServletResponse response, Object data, String msg) {
        Result result = new Result();
        result.setCode(ProjectStatusEnum.SUCCESS.getCode());
        result.setData(data);
        result.setMessage(msg);
        write(response, result);
    }

    public static void writeSuccessResult(HttpServletResponse response) {
        Result result = new Result();
        result.setCode(ProjectStatusEnum.SUCCESS.getCode());
        result.setData((Object) null);
        result.setMessage("成功");
        write(response, result);
    }

    public static void writeFailureResult(HttpServletResponse response, int code, String msg) {
        Result result = new Result();
        result.setCode(code);
        result.setData((Object)null);
        result.setMessage(msg);
        write(response, result);
    }

    public static void writeFailureResult(HttpServletResponse response, ProjectStatusEnum projectStatusEnum) {
        Result result = new Result();
        result.setCode(projectStatusEnum.getCode());
        result.setData((Object)null);
        result.setMessage(projectStatusEnum.getMsg());
        write(response, result);
    }

    private static void write(HttpServletResponse response, Object obj) {
        PrintWriter writer = null;
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json");

        try {
            writer = response.getWriter();
            writer.write(JSON.toJSONStringWithDateFormat(obj, "yyyy-MM-dd HH:mm:ss", new SerializerFeature[]{SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.WriteNullBooleanAsFalse, SerializerFeature.WriteNullNumberAsZero}));
        } catch (Exception var7) {
            var7.printStackTrace();
        } finally {
            if (null != writer) {
                writer.flush();
                writer.close();
            }

        }

    }
}
